﻿namespace EFS.RC.Presences.Controls
{
	public partial class MainWindow : System.Windows.Window
	{

		private System.Collections.Generic.List<EFS.RC.Presences.Controls.Lync.Presences.HuntGroup> HuntGroups = new System.Collections.Generic.List<EFS.RC.Presences.Controls.Lync.Presences.HuntGroup>();

		private System.Windows.Threading.DispatcherTimer _ReloadGroups;
		private System.Windows.Threading.DispatcherTimer _RefreshLogons;

		System.Collections.Generic.Dictionary<System.String, EFS.RC.Presences.Controls.Lync.Presences.PresenceGroupProperties> windowProperties =
				new System.Collections.Generic.Dictionary<string, Lync.Presences.PresenceGroupProperties>();

		System.Collections.Generic.Dictionary<System.String, EFS.RC.Presences.Controls.Lync.Presences.PresenceWindow> windows =
			new System.Collections.Generic.Dictionary<string, Lync.Presences.PresenceWindow>();

		System.String[] groupNames = null;

		public MainWindow()
		{
			InitializeComponent();

			//main window position
			Top = System.Windows.SystemParameters.FullPrimaryScreenHeight - Height;
			Left = System.Windows.SystemParameters.FullPrimaryScreenWidth - Width;

			//initialise windows for all groups
			groupNames = EFS.RC.Presences.Properties.Settings.Default.ActiveGroups.Split(new System.Char[] { '|' }, System.StringSplitOptions.RemoveEmptyEntries);

			//load settings for each group
			LoadWindowSettings();

			foreach (System.String groupName in groupNames)
			{
				if (! windowProperties.ContainsKey(groupName))
				{
					Lync.Presences.PresenceGroupProperties properties = new Lync.Presences.PresenceGroupProperties();

					properties.groupName = groupName;

					windowProperties.Add(groupName, properties);
				}

				//create window
				windows.Add(groupName, new Lync.Presences.PresenceWindow(windowProperties[groupName]));
			}
		}

		private void LoadWindowSettings()
		{
			System.String[] settingString = EFS.RC.Presences.Properties.Settings.Default.WindowProperties.Split(new System.Char[] { ';' }, System.StringSplitOptions.RemoveEmptyEntries);

			foreach (System.String setting in settingString)
			{
				System.String[] settings = setting.Split(new System.Char[] { '|' }, System.StringSplitOptions.RemoveEmptyEntries);
				windowProperties.Add(settings[0], new Lync.Presences.PresenceGroupProperties(setting));
			}
		}

		private void SaveWindowSettings()
		{

			System.String settingString = "";

			foreach (System.String groupName in groupNames)
			{
				if (!settingString.Equals("")) settingString += ";";
				settingString += windows[groupName].SaveProperties();
			}

			EFS.RC.Presences.Properties.Settings.Default["WindowProperties"] = settingString;
			EFS.RC.Presences.Properties.Settings.Default.Save();
		}

		private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			_ReloadGroups_Tick(this, new System.EventArgs());

			_ReloadGroups = new System.Windows.Threading.DispatcherTimer(System.Windows.Threading.DispatcherPriority.Background);
			_ReloadGroups.Interval = System.TimeSpan.FromMinutes(30);
			_ReloadGroups.Tick += new System.EventHandler(_ReloadGroups_Tick);
			_ReloadGroups.Start();

			_RefreshLogons = new System.Windows.Threading.DispatcherTimer(System.Windows.Threading.DispatcherPriority.Background);
			_RefreshLogons.Interval = System.TimeSpan.FromMinutes(1);
			_RefreshLogons.Tick += new System.EventHandler(_RefreshLogons_Tick);
			_RefreshLogons.Start();
		}

		void _RefreshLogons_Tick(object sender, System.EventArgs e)
		{
			foreach (EFS.RC.Presences.Controls.Lync.Presences.HuntGroup huntgroup in HuntGroups)
			{
				huntgroup.RefreshStatus();

				System.Collections.Generic.List<System.String> loggedInMembers = new System.Collections.Generic.List<System.String>();

				foreach (EFS.RC.Presences.Controls.Lync.Presences.HuntGroupMember member in huntgroup.Members)
				{
					if (member.LoggedIn)
					{
						loggedInMembers.Add(member.AgentSIP);
					}
				}

				if (huntgroup.HuntGroupName != null) windows[huntgroup.HuntGroupName].controlPresencesList.LoadPresences(loggedInMembers.ToArray());
			}
		}

		void _ReloadGroups_Tick(object sender, System.EventArgs e)
		{
			//read the groups that want to add to board
			System.String[] groupnames = EFS.RC.Presences.Properties.Settings.Default.ActiveGroups.Split(new System.Char[] { '|' }, System.StringSplitOptions.RemoveEmptyEntries);

			HuntGroups.Clear();

			foreach (System.String huntgroup in groupnames)
			{
				HuntGroups.Add(new EFS.RC.Presences.Controls.Lync.Presences.HuntGroup(huntgroup));
			}

			_RefreshLogons_Tick(this, new System.EventArgs());
		}

		private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			SaveWindowSettings();
			System.Environment.Exit(0);
		}
	}

}